package com.github.linkoog.model_designer.ide.editer.dbmodel.right_editer;

import com.github.linkoog.model_designer.ide.editer.common.Callback;
import com.github.linkoog.model_designer.ide.editer.dbmodel.domain.DatasourceNavigatable;
import com.github.linkoog.model_designer.ide.editer.dbmodel.right_editer.table.DatasourceJTable;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.util.OpenSourceUtil;
import org.jetbrains.yaml.YAMLElementGenerator;
import org.jetbrains.yaml.psi.*;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class DatasourceListBuilder {

    private static final Logger LOGGER = Logger.getInstance(DatasourceListBuilder.class);

    private final Project project;
    private final VirtualFile virtualFile;
    private final YAMLElementGenerator yamlElementGenerator;
    private Callback callback;

    private JPanel myMainPanel;
    private ToolbarDecorator toolbarDecorator;
    private DatasourceJTable datasourceJTable;

    public DatasourceListBuilder(Project project, VirtualFile virtualFile){
        this.project = project;
        this.virtualFile = virtualFile;
        this.yamlElementGenerator = YAMLElementGenerator.getInstance(project);

        initUiPanel();
    }

    private void initUiPanel(){
        datasourceJTable = new DatasourceJTable();

        datasourceJTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                final JTable table = (JTable) e.getSource();
                int row = table.rowAtPoint(e.getPoint()); //获得行位置
                int col = table.columnAtPoint(e.getPoint()); //获得列位置
                if (callback != null){
                    callback.callback();
                }

                if (row == -1 || col == -1){
                    table.clearSelection();
                    return;
                }

                if (col == 2){
                    //点击行中的编辑列
                    return;
                }

                DatasourceNavigatable datasourceNavigatable = datasourceJTable.getTableInfoModel().getRowData(row);
                OpenSourceUtil.navigate(true, datasourceNavigatable.getNavigatable());
            }
        });

        toolbarDecorator = ToolbarDecorator.createDecorator(datasourceJTable);
        /*toolbarDecorator.setAddAction(anActionButton -> {

        });

        toolbarDecorator.setRemoveAction(anActionButton -> {

        });*/

        myMainPanel = toolbarDecorator.createPanel();
    }

    public void resetDatasourceList() {
        YAMLFile yamlFile = (YAMLFile) PsiManager.getInstance(project).findFile(virtualFile);
        if (yamlFile == null){
            return;
        }

        final YAMLDocument root = yamlFile.getDocuments().get(0);
        if (root == null) {
            getDatasourceJTable().getTableInfoModel().reset(new ArrayList<>());
            return;
        }

        YAMLMapping rootMapping = PsiTreeUtil.findChildOfType(root, YAMLMapping.class);
        if (rootMapping == null) {
            getDatasourceJTable().getTableInfoModel().reset(new ArrayList<>());
            return;
        }

        YAMLKeyValue datasourceKeyValues = rootMapping.getKeyValueByKey("datasources");
        if (datasourceKeyValues == null ){
            getDatasourceJTable().getTableInfoModel().reset(new ArrayList<>());
            return;
        }

        YAMLValue commonColumnsValue = datasourceKeyValues.getValue();
        if ( !(commonColumnsValue instanceof  YAMLMapping)){
            getDatasourceJTable().getTableInfoModel().reset(new ArrayList<>());
            return;
        }

        List<DatasourceNavigatable> list = new ArrayList<>();
        YAMLMapping commonColumnsMapping = (YAMLMapping) commonColumnsValue;
        for (YAMLKeyValue yamlKeyValue : commonColumnsMapping.getKeyValues()) {
            String comment = "";
            final YAMLValue value = yamlKeyValue.getValue();
            if (value instanceof YAMLMapping){
                YAMLMapping mapping = (YAMLMapping)value;
                YAMLKeyValue commentKeyValue = mapping.getKeyValueByKey("description");
                if (commentKeyValue != null){
                    comment = commentKeyValue.getValueText();
                }
            }

            DatasourceNavigatable navigatable = new DatasourceNavigatable();
            navigatable.setName(yamlKeyValue.getKeyText());
            navigatable.setComment(comment);
            navigatable.setNavigatable(yamlKeyValue);
            list.add(navigatable);
        }
        getDatasourceJTable().getTableInfoModel().reset(list);
    }

    public JPanel getMyMainPanel() {
        return myMainPanel;
    }

    public DatasourceJTable getDatasourceJTable() {
        return datasourceJTable;
    }

    public Callback getCallback() {
        return callback;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }


}
